
import { CommandHandler, ICommandHandler, EventBus } from '@nestjs/cqrs'
import { Inject } from '@nestjs/common'
import { UserCreateCommand } from './user-create.command'
import { UserCreateEvent } from '../events/user-create.event'
import { UserEntity } from '@/user/entities/user.mysql.entity'
import { Repository } from 'typeorm'
@CommandHandler(UserCreateCommand)
export class UserCreateCommandHandler implements ICommandHandler<UserCreateCommand> {
  constructor(private readonly enventBus: EventBus,
    @Inject('USER_SUBSCRIBER')
    private readonly userSubscriber: UserEntity,
    @Inject('USER_REPOSITORY')
    private readonly userRepository: Repository<UserEntity>,) { }
  async execute(command: UserCreateCommand) {
    const user = await this.userRepository.save(command.params)
    this.enventBus.publish(new UserCreateEvent(user))
    return {
      inserId: user.id
    }
  }
}
